//
//  WaterFlowController.swift
//  SwiftDemo
//
//  Created by lw on 2018/9/5.
//  Copyright © 2018年 mjsfax. All rights reserved.
//

import UIKit

private let kContentCellID = "WaterFlowCell"

class WaterFlowController: UIViewController {
    
    fileprivate lazy var collectionView : UICollectionView = {
        let layout = WaterFlowLayout() //UICollectionViewFlowLayout()
        let margin : CGFloat = 10
//        let rowNum : CGFloat = 3
//        let itemWH : CGFloat = (view.bounds.width - (rowNum + 1) * margin) / rowNum
//        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        layout.minimumLineSpacing = margin
        layout.minimumLineSpacing = margin
        layout.sectionInset = UIEdgeInsetsMake(margin, margin, margin, margin)
        layout.dataSource = self
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.dataSource = self
//            collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.never
        return collectionView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(collectionView)
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: kContentCellID)

    }

}

extension WaterFlowController : UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 20
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kContentCellID, for: indexPath)
        cell.backgroundColor = UIColor.randomColor()
        return cell
    }
}

extension WaterFlowController : WaterFlowLayoutDataSource {
    func numberOfCols(_ waterFlow: WaterFlowLayout) -> Int {
        return 3
    }
    
    func waterFlow(_ waterFlow: WaterFlowLayout, item: Int) -> CGFloat {
        return CGFloat(arc4random_uniform(150) + 120)
    }
}


